package com.zhang.cicadatravels.main;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.zhang.cicadatravels.R;
import com.zhang.cicadatravels.base.BaseActivity;
import com.zhang.cicadatravels.main.box.ToolBoxFragment;
import com.zhang.cicadatravels.main.guides.GuidesFragment;
import com.zhang.cicadatravels.main.search.SearchActivity;
import com.zhang.cicadatravels.main.travels.TravelsFragment;

import java.util.ArrayList;


public class MainActivity extends BaseActivity implements View.OnClickListener {

    private ViewPager mainActivityViewPager;
    private TabLayout mainActivityTabLayout;
    private MainAdapter mainAdapter;
    private ArrayList<Fragment> fragments;
    private ImageView titlesSearchIcon;
    private ImageView titlesUserIcon;
    private ImageView titlesMenuIcon;

    public int initcreate() {
        return R.layout.activity_main;
    }


    protected void initview() {
        mainActivityViewPager = (ViewPager) findViewById(R.id.activity_main_viewpager);
        mainActivityTabLayout = (TabLayout) findViewById(R.id.activity_main_tablayout);
        titlesMenuIcon = (ImageView) findViewById(R.id.activity_main_titles_menu_iv);
        titlesSearchIcon = (ImageView) findViewById(R.id.activity_main_titles_search_iv);
        titlesUserIcon = (ImageView) findViewById(R.id.activity_main_titles_user_iv);
        mainAdapter = new MainAdapter(getSupportFragmentManager());
        fragments = new ArrayList<>();
    }

    protected void initdata() {
        fragments.add(new TravelsFragment());
        fragments.add(new GuidesFragment());
        fragments.add(new ToolBoxFragment());


        titlesSearchIcon.setOnClickListener(this);
        titlesUserIcon.setOnClickListener(this);
        titlesMenuIcon.setOnClickListener(this);

        mainAdapter.setFragments(fragments);
        mainActivityViewPager.setAdapter(mainAdapter);
        mainActivityTabLayout.setupWithViewPager(mainActivityViewPager);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_main_titles_search_iv:
                Intent searchIntent = new Intent(MainActivity.this, SearchActivity.class);
                startActivity(searchIntent);
                break;
            case R.id.activity_main_titles_user_iv:
                Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(loginIntent);
                break;
            case R.id.activity_main_titles_menu_iv:
                popview();
                break;
            case R.id.popup_out_line:

                break;
            case R.id.popup_feedback:

                break;
            case R.id.popup_set_up:

                break;
        }
    }

    private void popview() {
        PopupWindow popupWindow = new PopupWindow(420, ViewGroup.LayoutParams.WRAP_CONTENT);
        View view = LayoutInflater.from(this).inflate(R.layout.popup_view_activity_main, null);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setContentView(view);
        popupWindow.showAsDropDown(titlesMenuIcon, 100, 30);
        TextView outLine = (TextView) view.findViewById(R.id.popup_out_line);
        TextView feedBack = (TextView) view.findViewById(R.id.popup_feedback);
        TextView setUp = (TextView) view.findViewById(R.id.popup_set_up);
        outLine.setOnClickListener(this);
        feedBack.setOnClickListener(this);
        setUp.setOnClickListener(this);
    }
}
